package pt.fct.mpc.lastfm.artist;

import java.util.List;

import pt.fct.mpc.lastfm.R;
import pt.fct.mpc.lastfm.database.LastfmDatabaseFavourites;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class ArtistActivity extends TabActivity {

	private static final String TAG = "Last.fm Artist Activity";
	ArtistData artistDataSet = new ArtistData();
	String artist_name = null;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// Reads the info sent by the intent
		Intent the_intent_that_gave_life_to_everything = getIntent();
		Bundle intent_data = the_intent_that_gave_life_to_everything
				.getExtras();

		if (intent_data == null) {
			Log.e(TAG, "No extra data");
			finish();
		}

		String data_artist_name = intent_data.getString("name");
		if (data_artist_name == null) {
			Log.e(TAG, "Null bundle parameters: artist name = "
					+ data_artist_name);
			finish();
		}

		artistDataSet.setName(data_artist_name);

		Bundle TabsData = new Bundle();
		TabsData.putSerializable("artistserial", artistDataSet);

		Intent ArtistAlbumsActivityIntent = new Intent(this, ArtistAlbumsActivity.class);
		ArtistAlbumsActivityIntent.putExtras(TabsData);

		Intent ArtistEventsActivityIntent = new Intent(this, ArtistEventsActivity.class);
		ArtistEventsActivityIntent.putExtras(TabsData);
		
		Intent ArtistImagesActivityIntent = new Intent(this, ArtistImagesActivity.class);
		ArtistImagesActivityIntent.putExtras(TabsData);
		
		Intent ArtistInfoActivityIntent = new Intent(this, ArtistInfoActivity.class);
		ArtistInfoActivityIntent.putExtras(TabsData);
		
		// Creates the tabs and its content
		TabHost tabHost = getTabHost();
		tabHost.addTab(tabHost.newTabSpec("Artist").setIndicator("Artist")
				.setContent(ArtistInfoActivityIntent));
		tabHost.addTab(tabHost.newTabSpec("Albums").setIndicator("Albums")
				.setContent(ArtistAlbumsActivityIntent));
		tabHost.addTab(tabHost.newTabSpec("Events").setIndicator("Events")
				.setContent(ArtistEventsActivityIntent));
		tabHost.addTab(tabHost.newTabSpec("Images").setIndicator("Images")
				.setContent(ArtistImagesActivityIntent));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.artist_options_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		LastfmDatabaseFavourites db_favourites = new LastfmDatabaseFavourites(
				this);

		switch (item.getItemId()) {
		case R.id.artist_options_menu_add_favourite:

			db_favourites.addArtist(artistDataSet.getName());
			Toast.makeText(this, "Added to Favourites", Toast.LENGTH_SHORT)
					.show();
			break;
		}

		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.artist_context_menu_albums, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();

		int list_position = (int) info.id;

		if (item.getItemId() == R.id.artist_context_menu_add_favourite) {
			List<AlbumData> album_list = artistDataSet.getAlbums();
			if ((album_list == null) || (album_list.size() <= list_position))
				return false;

			AlbumData album_data = album_list.get(list_position);
			if (album_data == null)
				return false;

			String album_artist = album_data.getArtistName();
			String album_name = album_data.getAlbumName();

			LastfmDatabaseFavourites db_favourites = new LastfmDatabaseFavourites(
					this);

			db_favourites.addAlbum(album_artist, album_name);

			Toast.makeText(this, "Album added to favourites",
					Toast.LENGTH_SHORT).show();
		}

		return true;
	}
}